package common.utility;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;

import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class UtilityImage {

	public static final int SAME = -1;
	public static final int RATIO = 0;
	
	public static void resize(File src, File dest, int width, int height) throws IOException {
		FileInputStream srcIs = null;
		try {
			srcIs = new FileInputStream(src);
			UtilityImage.resize(srcIs, dest, width, height);
		} finally {
			if (srcIs != null) try { srcIs.close(); } catch(IOException ex) {}
		}
	}

	public static void resize(InputStream src, File dest, int width, int height) throws IOException {
		BufferedImage srcImg = ImageIO.read(src);
		int srcWidth = srcImg.getWidth();
		int srcHeight = srcImg.getHeight();
		
		int destWidth = -1, destHeight = -1;
		
		if(srcWidth > width){
		
			if (width == SAME) {
				destWidth = srcWidth;
			} else if (width > 0) {
				destWidth = width;
			}
	
			if (height == SAME) {
				destHeight = srcHeight;
			} else if (height > 0) {
				destHeight = height;
			}
			
			if (width == RATIO && height == RATIO) {
				destWidth = srcWidth;
				destHeight = srcHeight;
			} else if (width == RATIO) {
				double ratio = ((double)destHeight) / ((double)srcHeight);
				destWidth = (int)((double)srcWidth * ratio);
			} else if (height == RATIO) {
				double ratio = ((double)destWidth) / ((double)srcWidth);
				destHeight = (int)((double)srcHeight * ratio);
			}
		}else{
			destWidth = srcWidth;
			destHeight = srcHeight;
			
		}
		BufferedImage destImg = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_3BYTE_BGR);
		Graphics2D g = destImg.createGraphics();
		g.drawImage(srcImg, 0, 0, destWidth, destHeight, null);
		
		ImageIO.write(destImg, "jpg", dest);
    }
	
	
	public static Map<String, Integer> viewImg(String upDir, String src, int width, int height) throws IOException{
		Map<String, Integer> imgsize = new HashMap<String, Integer>();
		
		File file = new File(upDir, src);
		if(file.exists()){
			BufferedImage srcImg = ImageIO.read(file);
			
			int srcWidth = srcImg.getWidth();
			int srcHeight = srcImg.getHeight();
			
			int destWidth = -1, destHeight = -1;
			
			if(srcWidth > width){
				if (width == SAME) {
					destWidth = srcWidth;
				} else if (width > 0) {
					destWidth = width;
				}
	
				if (height == SAME) {
					destHeight = srcHeight;
				} else if (height > 0) {
					destHeight = height;
				}
				
				if (width == RATIO && height == RATIO) {
					destWidth = srcWidth;
					destHeight = srcHeight;
				} else if (width == RATIO) {
					double ratio = ((double)destHeight) / ((double)srcHeight);
					destWidth = (int)((double)srcWidth * ratio);
				} else if (height == RATIO) {
					double ratio = ((double)destWidth) / ((double)srcWidth);
					destHeight = (int)((double)srcHeight * ratio);
				}
				
				imgsize.put("width", destWidth);
				imgsize.put("height", destHeight);
			}else{
				imgsize.put("width", srcWidth);
				imgsize.put("height", srcHeight);
			}
		}else{
			imgsize = null;
		}
		
		
		return imgsize;
	}

}